package com.wofish.member.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.wofish.member.domain.MemberLv;
import com.wofish.member.domain.MemberLvVo;
import com.wofish.member.mapper.MemberLvMapper;
import com.wofish.member.service.IMemberLvService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.wofish.common.core.text.Convert;

/**
 * 会员等级 服务层实现
 * 
 * @author wofish
 * @date 2019-04-21
 */
@Service
public class MemberLvServiceImpl implements IMemberLvService {
	@Autowired
	private MemberLvMapper memberLvMapper;

	/**
     * 查询会员等级信息
     * 
     * @param memberLvId 会员等级ID
     * @return 会员等级信息
     */
    @Override
	public MemberLv selectMemberLvById(Long memberLvId) {
	    return memberLvMapper.selectMemberLvById(memberLvId);
	}

	@Override
	public List<MemberLvVo> queryMemberLvList(MemberLv memberLv) {
		List<MemberLv> list = memberLvMapper.selectMemberLvList(memberLv);
		List<MemberLvVo> result = null;
		if(CollectionUtils.isNotEmpty(list)) {
			result = list.stream().map(lv -> {
				MemberLvVo vo = new MemberLvVo();
				vo.setCreateTime(lv.getCreateTime());
				vo.setDefaultLv(lv.getDefaultLv());
				vo.setDisabled(lv.getDisabled());
				vo.setDisCount(lv.getDisCount());
				vo.setExpireTime(lv.getExpireTime());
				vo.setExpireTimeUnit(lv.getExpireTimeUnit());
				vo.setLogo(lv.getLogo());
				vo.setMemberLvId(lv.getMemberLvId());
				vo.setName(lv.getName());
				vo.setPoint(lv.getPoint());
				vo.setPreId(lv.getPreId());
				if(null != lv.getPreId()) {
					MemberLv preLv = memberLvMapper.selectMemberLvById(lv.getPreId());
					vo.setPreName(preLv.getName());
				}
				vo.setRemark(lv.getRemark());
				vo.setUpdateTime(lv.getUpdateTime());
				return vo;
			}).collect(Collectors.toList());
		}
		return result;
	}

	/**
     * 查询会员等级列表
     *
     * @param memberLv 会员等级信息
     * @return 会员等级集合
     */
	@Override
	public List<MemberLv> selectMemberLvList(MemberLv memberLv) {
	    return memberLvMapper.selectMemberLvList(memberLv);
	}

    /**
     * 新增会员等级
     * 
     * @param memberLv 会员等级信息
     * @return 结果
     */
	@Override
	public int insertMemberLv(MemberLv memberLv) {
		Date date = new Date();
		memberLv.setCreateTime(date);
		memberLv.setUpdateTime(date);
	    return memberLvMapper.insertMemberLv(memberLv);
	}
	
	/**
     * 修改会员等级
     * 
     * @param memberLv 会员等级信息
     * @return 结果
     */
	@Override
	public int updateMemberLv(MemberLv memberLv) {
		memberLv.setUpdateTime(new Date());
	    return memberLvMapper.updateMemberLv(memberLv);
	}

	/**
     * 删除会员等级对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteMemberLvByIds(String ids) {
		return memberLvMapper.deleteMemberLvByIds(Convert.toStrArray(ids));
	}

	@Override
	public Map<Long, String> getEnableLvName() {
		MemberLv memberLv = new MemberLv();
		memberLv.setDisabled(0);
		List<MemberLv> list = memberLvMapper.selectMemberLvList(memberLv);
		if(CollectionUtils.isEmpty(list)) {
			return new HashMap<>(2);
		}
		return list.stream().collect(Collectors.toMap(MemberLv::getMemberLvId, MemberLv::getName));
	}
	
}
